package mylittleinvestigations;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.UnicodeFont;
import org.newdawn.slick.font.effects.ColorEffect;

public class Utils
{
    public static <T> T as(Object o,Class<T> t)
    {
        return t.isInstance(o) ? t.cast(o) : null;
    }

    public static boolean isNullOrEmpty(String s)
    {
        if (s == null)
        {
            return true;
        }
        
        return s.isEmpty();
    }

    public static boolean isNullOrWhiteSpace(String s)
    {
        if (s == null)
        {
            return true;
        }
        
        int length = s.length();
        
        if (length > 0)
        {
            for (int i = 0; i < length; i++)
            {
                if (!Character.isWhitespace(s.charAt(i)))
                {
                    return false;
                }
            }
            
            return true;
        }
        
        return true;
    }
    
    public static boolean equals(Object o1, Object o2)
    {
        return
            (o1 == null && o2 == null) ||
            (o1 != null && o2 != null && o1.equals(o2));
    }
    
    public static void drawImage(Graphics g, Image image, Vector2 position)
    {
        g.drawImage(image, (float)position.getX(), (float)position.getY());
    }
    
    public static void drawString(UnicodeFont font, String string, Vector2 position, Color color)
    {
        font.drawString((float)position.getX(), (float)position.getY(), string, color);
    }
    
    public static UnicodeFont getUnicodeFont(java.awt.Font font, int fontSize, boolean bold, boolean italic) throws SlickException
    {
        UnicodeFont unicodeFont = new UnicodeFont(font, fontSize, bold, italic);
        unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
        unicodeFont.addNeheGlyphs();
        unicodeFont.loadGlyphs();
        
        return unicodeFont;
    }
    
    public static UnicodeFont getUnicodeFont(String fontPath, int fontSize, boolean bold, boolean italic) throws SlickException
    {
        UnicodeFont unicodeFont = new UnicodeFont(fontPath, fontSize, bold, italic);
        unicodeFont.getEffects().add(new ColorEffect(java.awt.Color.WHITE));
        unicodeFont.addNeheGlyphs();
        unicodeFont.loadGlyphs();
        
        return unicodeFont;
    }
}
